import { ui } from "./ui/layaMaxUI";
import GameWx from "./GameWx";
import GameEvent from "./GameEvent";
import GameCore from "./GameCore";
import WxMember from "./WxObje/WxMember";
import Gstatic from "./gameobj/Gstatic";

export default class HomeUI extends ui.HomeUI {
    private static _i_homeui: HomeUI;
    public static get I() {
        if (!this._i_homeui) {
            this._i_homeui = new HomeUI();
        }
        return this._i_homeui;
    }
    private constructor() {
        super()
        this.btn0.on(Laya.Event.CLICK, this, this.click0);

        this.btn_test.on(Laya.Event.CLICK, this, this.click_test);
        this.graphics.drawLine(85, 1260, 85, 800, "#00ff00");
        this.graphics.drawLine(85, 800, 665, 800, "#00ff00");
        this.graphics.drawLine(665, 800, 665, 1260, "#00ff00");

        var yy = 80 + 460;
        this.graphics.drawLine(85, 80, 85, yy, "#00ff00");
        this.graphics.drawLine(85, yy, 665, yy, "#00ff00");
        this.graphics.drawLine(665, yy, 665, 80, "#00ff00");

        this.btn_test.visible = true;
        this.btn0.visible     = false;
        GameWx.I.on(GameEvent.Event_WX_Game_Start, this, this.gamestart);
        if (GameWx.I.accessInfo == "") {
            this.p1.skin = GameWx.I.userinfo.avatarUrl;
            this.btn0.visible = true;
            this.btn2.visible = false;
        } else {
            this.p2.skin = GameWx.I.userinfo.avatarUrl;
            this.btn2.label = "OK";
            this.btn2.visible = true;
            GameWx.I.on(GameEvent.Event_WX_Room_Success, this, this.wxroom);
            GameWx.I.joinRoom();
        }
    }

    gamestart(){
        this.label0.visible = false;
        this.label1.visible = false;
        this.label2.visible = false;
        this.player_ui.visible = false;
    }

    click_p2() {
        GameWx.I.shareAppMessage();
    }

    click_test() {
        //房主player1 , 客人player2
        GameWx.I.uploadFrame([GameWx.I.my.role==1?"0,1":"0,2"]);
    }

    click0() {
        this.btn0.visible = false;
        GameWx.I.on(GameEvent.Event_WX_Room_Success, this, this.wxroom);
        GameWx.I.getRoom();
    }

    click1() {
        this.btn0.visible = false;
        this.player_ui.visible = false;
        GameWx.I.startGame();
    }

    wxroom() {
        GameWx.I.on(GameEvent.Event_WX_SyncFrame, this, this.sf);
        
        console.log(" wxroom " , GameWx.I.roominfo);
        var arr = GameWx.I.roominfo.memberList;
        var my:WxMember;
        for (let i = 0; i < arr.length; i++) {
            const e = arr[i] as WxMember;
            if(e.clientId+"" == GameWx.I.clientId){
                my = e;
                break;
            }
        }

        this.btn0.label = "开始游戏";
        if(my.role == 1 && arr.length==2){
            this.btn0.visible = true;
            this.btn0.off(Laya.Event.CLICK, this, this.click0);
            this.btn0.on(Laya.Event.CLICK, this, this.click1); 

            this.p2.on(Laya.Event.CLICK, this, this.click_p2);                      
            this.btn2.label = "OK";
        }
        else if(my.role == 1 && arr.length<2){
            this.btn0.visible = true;
            this.btn0.visible = true;
            this.btn0.off(Laya.Event.CLICK, this, this.click0);
            this.btn0.on(Laya.Event.CLICK, this, this.click1); 
            
            this.btn2.visible = true;
            this.p2.on(Laya.Event.CLICK, this, this.click_p2);            
            this.btn2.label = "邀请";
        }


        if(arr[0]){
            this.p1.skin = (arr[0] as WxMember).headimg;
            //this.btn1.label = "OK";
        }
        if(arr[1]){
            this.p2.skin = (arr[1] as WxMember).headimg;
            //this.btn1.label = "OK";
        }

    }

    sf(res) {
        GameCore.I.syncFrame(res);
    }
}